<template>
  <el-dialog
    title="分配权限"
    :visible="visibleDialog"
    @close="handleColse"
  >
    <el-tree
      ref="permTree"
      :data="permData"
      :props="defaultProps"
      show-checkbox
      :default-expand-all="true"
      :default-checked-keys="selectCheck"
      node-key="id"
      :check-strictly="true"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="handleSubmit">确定</el-button>
        <el-button size="small" @click="handleColse">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  name: 'ManagerPermission',
  components: {},
  props: {
    visibleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      permData: [],
      defaultProps: {
        label: 'name'
      },
      selectCheck: []

    }
  },
  computed: {},
  watch: {
    async visibleDialog(val) {
      if (val) {
        await this.getPermissionList()
        await this.getRoleDetail()
      }
    }
  },
  created() {},
  methods: {
    handleColse() {
      this.$emit('update:visibleDialog', false)
    },
    async getPermissionList() {
      const res = await getPermissionList()
      this.permData = tranListToTreeData(res, '0')
    },
    async getRoleDetail() {
      const res = await getRoleDetail(this.$attrs['role-id'])
      this.selectCheck = res.permIds
    },
    async handleSubmit() {
      await assignPerm(
        { permIds: this.$refs.permTree.geCheckdkeys(),
          id: this.$attrs['role-id'] }
      )
      this.$message.success('分配成功')
      this.handleColse()
    }
  }
}
</script>

<style scoped>

</style>
